<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\Goods;
use App\Http\Model\Type;
use Input;

class PublishController extends Controller
{
    /**
     * link方法
     */
    public function pub2(){
            $link = \DB::table('sad_link')->orderBy('order','asc')->get();
            return $link;
        }
   /**
     * 发布商品页面
     */
    public function publish(){
        // 获取所有父分类
        $type = Type::all();
        foreach ($type as $key => $value) {
            if($value['pid']==0){
                $ptype[] = $value;
            }
        }
        $link = $this -> pub2();
        //返回页面
        return view('home.user.publish',compact('ptype','link'));
    }

    /**
     * 商品修改页面
     */
    public function edit(){
        // return $_GET['id'];
        // 获取所有父分类
        $type = Type::all();
        foreach ($type as $key => $value) {
            if($value['pid']==0){
                $ptype[] = $value;
            }
        }
        //获取当前商品的信息
        $good = Goods::find($_GET['id']);
         $link = $this -> pub2();
        return view('/home/user/publishedit',compact('ptype','good','link'));
    }

    /**
     * 执行商品修改
     */
    public function doEdit(Request $request){
        $data = $request->except('_token','id');
        $res = Goods::where('id',$_GET['id'])->update($data);
        if($res){
            return redirect('/user');
        }else{
            return back()->whth('errors','修改失败请重新修改');
        }
    }

    /**
     * 执行商品修改
     */
    public function del(Request $request){
        
        $res = Goods::find($request['id'])->delete();
        if($res){
            return redirect('/user');
        }else{
            return back()->whth('errors','删除失败请重新删除');
        }
    }


    /**
     * ajax获取子分类
     */
    public function sid(Request $request){
        //获取当前分类的子分类
        $pid = $request->all();
        $stype = Type::where('pid',$pid['pid'])->get()->toArray();
        // 定义存储标签的变量
        $option = '<option  value="0">--请选择--</option>';
        foreach ($stype as $key => $value) {
            $option .= '<option  value="'.$value['id'].'">'.$value['name'].'</option>';
        }
        return $option;
    }

    /**
     * 执行商品发布动作
     */
    public function doPublish(Request $request){
        //获取发布的信息
        $info = $request->except('_token','file_upload');
        // dd($info);
        //地址
        $data['address'] = $info['province'].'-'.$info['country'].'-'.$info['town'];
        //商品名
        $data['title'] = $info['title'];
        //用户id
        $data['uid'] = session('huser')['id'];
        //产品分类
        $data['tid'] = $info['stype'];
        //商品描述
        $data['intro'] = $info['intro'];
        //商品成色
        $data['condition'] = $info['condition'];
        //原价
        $data['price'] = $info['price'] =='' ? 0 : $info['price'];
        //现价
        $data['reprice'] = $info['reprice'];
        //图片
        $data['pic'] = $info['pic'];
        //图片
        $data['status'] = '出售中';
        // dd($data);
        $res = Goods::create($data);
        //发布时间
        // dd($res);
        if($res){
            return redirect('/user');
        }else{
            return back()->with('errors','发布失败,请重新发布');
        }
    }

//    文件上传
    public function upload(Request $request)
    {
//        获取文件对象
        $file = Input::file('file_upload');
        // dd($request->all());
//        判断文件是否有效
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;
            $path = $file->move(public_path().'/uploads/goods',$newName);
            $filepath = 'uploads/goods/'.$newName;
            return  $filepath;
        }
    }

}
